//
//  PinJieView.m
//  PingTu
//
//  Created by Yangfan on 15/2/5.
//  Copyright (c) 2015年 4gread. All rights reserved.
//

#import "PinJieView.h"
#import "FilesOperation.h"

@implementation PinJieView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    }
    return self;
}

- (void)setAssetsIntoPintuView:(NSArray *)assets {
    _assets = assets;
    [self UI];
}

- (void)UI {
    // 底部的模板滚动
    _pinJieBottomScrollView = [[PinJieBottomScrollView alloc] initWithFrame:CGRectMake(0, viewHeight - 50, viewWidth, 50)];
    [_pinJieBottomScrollView setShowsHorizontalScrollIndicator:NO];
    [self addSubview:_pinJieBottomScrollView];
    
    // 添加图片
    pinjiedic = [NSDictionary dictionaryWithContentsOfFile:[FilesOperation returnResourceFileAddress:@"pinjie" andTyeo:@"plist"]];
    NSDictionary *test;
    UIImageView *imageView;
    UIImage *image;
    UIButton *button;
    for (int i = 0; i < pinjiedic.count; i++) {
        test = [pinjiedic objectForKey:[NSString stringWithFormat:@"%d", i]];
        image = [UIImage imageNamed:[test objectForKey:@"headerImageName"]];
        int space = 7;
        if (i == 0) {
            space = 3;
        }
        imageView = [[UIImageView alloc] initWithFrame:CGRectMake(imageView.frame.origin.x + imageView.frame.size.width + space, 6, image.size.width * 38 / image.size.height, 38)];
        [imageView setUserInteractionEnabled:YES];
        [imageView setImage:image];
        [_pinJieBottomScrollView addSubview:imageView];
        
        button = [[UIButton alloc] initWithFrame:imageView.bounds];
        [button setTag:i];
        [button addTarget:self action:@selector(whichMuBan:) forControlEvents:UIControlEventTouchUpInside];
        [imageView addSubview:button];
    }
    
    [_pinJieBottomScrollView setContentSize:CGSizeMake(imageView.frame.origin.x + imageView.frame.size.width + 3, 50)];
    
    _pinJieContentScrollView = [[PinJieContentScrollView alloc] initWithFrame:CGRectMake(0, 0, viewWidth, viewHeight - 50)];
    [_pinJieContentScrollView setAssetsToImageArr:_assets];
    [_pinJieContentScrollView createAndUpdateUI:0 andDic:pinjiedic];
    [self addSubview:_pinJieContentScrollView];
}

- (void)whichMuBan:(UIButton *)sender {
    [_pinJieContentScrollView updateHeaderAndBackgroundColor:[self returnIntFromNSUInteger:sender.tag] andDic:pinjiedic];
    [_pinJieContentScrollView updateFooter:[self returnIntFromNSUInteger:sender.tag] andDic:pinjiedic];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
